<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Portal extends CI_Controller {
        
        public function index() { 
            
            // Security //
            if($this->session->userdata('isLoggedIn')) {
                $data['styles'] = array('crm/portal');
                $data['scripts'] = array('jquery.dataTables.min','datatables.reloadajax','portal','selectOverview', 'selectPage');
                              
                $this->load->view('crm/portal', $data);                
            }
            else {
                redirect('login');
            }
	}
        
        public function ajaxTab($i) {
            $this->load->view('crm/ajaxTab'.$i);
        }
        
        public function ajaxOverview($name) {
            $partner = new Partner();
            $partner->get();

            $data['partners'] = $partner;
            
            $this->load->view('crm/'.$name, $data);
        }
        
        public function addBacklink() {
            $partner_id = $this->input->post('partner_id');
            $link = $this->input->post('link');
            $description = $this->input->post('description');
                               
            if(empty($partner_id) || empty($link) || empty($description)) {
               $data['error'] = 'Please fill in all required fields.';
                echo json_encode($data);
            }
            else {
                $backlink = new Backlink();
            
                $backlink->partner_id = $partner_id;
                $backlink->link = $link;
                $backlink->description = $description;
                
                $backlink->save();
                
                $data['success'] = 'Backlink succesfully saved.';
                echo json_encode($data);               
            }
        }
        
        public function getBacklinks() {           
            $backlink = new Backlink();
            
            $backlink->get();
            foreach($backlink as $b) {
                $ajaxReturn[] = array($b->partner->name,$b->link,$b->description);
            }
           
            $data = array('aaData' => $ajaxReturn);

            echo json_encode($data);
        }
        
        /*public function getAppointments() {           
            $appointment = new Appointment();
            
            $appointment->get();
            foreach($appointment as $a) {
                $ajaxReturn[] = array($a->date,$a->location,$a->subject,$a->appointment_status->name);
            }
           
            $data = array('aaData' => $ajaxReturn);

            echo json_encode($data);
        }*/
        
        public function getRecords(){
            $record = new Record();
            
            $record->get();
            foreach ($record as $r){
                 $ajaxReturn[] = array($r->record_type->name,$r->created);
            }
            $data = array('aaData' => $ajaxReturn);
            //$data = array('aaData' =>'meuk');
            echo json_encode($data);
        }
        
        public function addRecord(){
            $partner_id = $this->input->post('partner_id');
            $person_id = $this->input->post('person_id');
            $record_type = $this->input->post('record_type');
            $file = $this->input->post('file');
            $created = $this->input->post('created');
            $communication_type = $this->input->post('communication_type');
            $description = $this->input->post('description');
                               
            if(empty($description)) {
               $data['error'] = 'Please fill in all required fields.';
                echo json_encode($data);
            }
            else {
                $record = new Record();
                
                $record->partner_id = $partner_id;
                $record->person_id = $person_id;
                $record->record_type = $record_type;
                $record->file = $file;
                $record->created = now();
                $record->communication_type = $communcation_type;
                $record->description = $description;
                
                
                $record->save();
                
                $data['success'] = 'Record succesfully saved.';
                echo json_encode($data);               
            }             
        }
        
        public function getPersons(){
            $person = new Person();
            $person->get();
            
            foreach($person as $p){
                $ajaxReturn[] = array($p->name,$p->last_name,$p->partner->name,$p->job_description, $p->phone_number, $p->email_address);
            }
            $data = array('aaData' =>$ajaxReturn);
            echo json_encode($data);
        }
        
        public function addPerson(){
            
            $person = new Person();
            $name = $this->input->post('name');
            $last_name = $this->input->post('last_name');
            $email_address = $this->input->post('email_address');
            $job_description = $this->input->post('job_description');
            $role_id = $this->input->post('role_id');
            $leading = $this->input->post('leading');
            $phone_number = $this->input->post('phone_number');
            $partner_id = $this->input->post('partner_id');
            $password = $this->input->post('password');
            $picture = $this->input->post('picture');
            $postalcode = $this->post('postalcode');
            $address = $this->post('address');
            $city = $this->post('city');
            $extra = $this->post('extra');
            $facebook = $this->post('facebook');
            $twitter = $this->post('twitter');
            $linkedin = $this->post('linkedin');
            $other = $this->post('other');

            if(empty($name) || empty($last_name) ||empty($email_address) || 
                    empty($job_description) ||empty($role_id) ||empty($phone_number)||empty($partner_id)||
                    empty($postalcode) ||empty($password)||empty ($city)||empty($address)){
                $data['error'] = 'Please fill in all required fields.';
                echo json_encode($data);
            
            }
            else{
            $person->name = $name;
            $person->last_name = $last_name;
            $person->email_address = $email_address;
            $person->job_description = $job_description;
            $person->role_id = $rol_id;
            $person->leading = $leading;
            $person->address = $address;
            $person->city = $city;
            $person->extra = $extra;
            $person->partner_id = $partner_id;
            $person->phone_number = $phone_number;
            $person->picture = $picture;
            $person->password = MD5($password);
            $person->postalcode = $postalcode;
            $person->twitter = $twitter;
            $person->facebook = $facebook;
            $person->linkedin = $linkedin;
            $person->other = $other;
            
            $person->save();
            
            $data['success'] = 'Person succesfully saved.';
            echo json_encode($data); 
            }
            
      
        }
        
        public function addAppointment(){
            $subject = $this->input->post('subject');
            $location = $this->input->post('location');
            $date = $this->input->post('date');
            $status_id = $this->input->post('status_id');
            $description = $this->input->post('description');
            
            if(empty($subject) || empty($location) ||empty($date) || empty($status_id) || empty ($description)){
                   $data['error'] = 'Please fill in all required fields.';
                echo json_encode($data);
            }
            else{
                $appointment = new Appointment();
                
                $appointment->subject = $subject;
                $appointment->location = $location;
                $appointment->date = $date;
                $appointment->status_id = $status_id;
                $appointment->description = $description;
                
                $appointment->save();
                
                  $data['success'] = 'Appointment succesfully saved.';
            echo json_encode($data);
            }
        }
        
        public function getAppointments(){
            
            $appointment = new Appointment();
            $appointment->get();
            
            foreach($appointment as $a){
                $ajaxReturn[] = array($a->date, $a->location, $a->subject, $a->id);
            }
           
            $data = array('aaData' =>$ajaxReturn); 
            echo json_encode($data);
        
        }
        
        public function getPartners(){
            $partner = new Partner();
            $partner->get();
            
            foreach($partner as $p){
                $ajaxReturn[] = array($p->name,$p->post_street_number,$p->post_postalcode,$p->post_city, $p->phone_number, $p->website, $p->email_address);
            }
            $data = array('aaData' =>$ajaxReturn);
            echo json_encode($data);
        }
}